package com.pan.mvc;

import com.pan.anno.MyService;
import com.pan.anno.MyTransactional;
import com.pan.anno.Seed;
import com.pan.aop.DatabaseHelper;
import com.pan.seed.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2020/11/24
 */
@Seed
@MyService
public class UserService implements IUserService {

    /**
     * 获取所有用户
     */
    @Override
    public List<User> getAllUser() {
        String sql = "SELECT * FROM user";
        return DatabaseHelper.queryEntityList(User.class, sql);
    }

    /**
     * 根据id获取用户信息
     */
    @Override
    public User GetUserInfoById(Integer id) {
        String sql = "SELECT * FROM user WHERE id = ?";
        return DatabaseHelper.queryEntity(User.class, sql, id);
    }

    /**
     * 修改用户信息
     */
    @MyTransactional
    @Override
    public boolean updateUser(int id, Map<String, Object> fieldMap) {
        return DatabaseHelper.updateEntity(User.class, id, fieldMap);

    }

    @MyTransactional
    public boolean updateUserWithoutDB() {

        System.out.println("-------------------updateUserWithoutDB-------------------------");
        return true;

    }

    @Override
    public List<User> getAllUserWithoutDB() {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "Tom", 22));
        userList.add(new User(2, "Alic", 12));
        userList.add(new User(3, "Bob", 32));
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userList;
    }
}
